#!/bin/bash

export PATH=/VI/mysql/bin:$PATH
export LD_LIBRARY_PATH=/VI/mysql/lib:$LD_LIBRARY_PATH

CURR_DIR=$(cd `dirname $0`; pwd)
APP_NAME="mysql"
PROGRAM=$CURR_DIR/dog
CRONTAB_CMD="*/1 * * * * $PROGRAM start"


#创建定时任务
count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`
if [ $count -lt 1 ]; then
    (crontab -l 2>/dev/null | grep -Fv $PROGRAM; echo "$CRONTAB_CMD") | crontab -
    count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`
    if [ $count -lt 1 ]; then
        echo "create cron faild."
        exit 1
    fi
fi

function pid() {
	ser=`ps -ef |grep -w "$CURR_DIR/$APP_NAME" |grep -v grep |awk '{print $2}'`
	echo $ser >"$CURR_DIR/${APP_NAME}.pid"
}

function start() {
        pid
        if [ "$ser" != "" ];then
                echo "$APP_NAME running. pid=$ser"
        else
			service mysql start
        fi
}

function stop() {
	pid
	echo stop $APP_NAME
	sleep 1
	if [ "$ser" != "" ];then
		service mysql stop
	fi
}

function restart() {
	service mysql restart
}

function status() {
	service mysql status
}

function help() {
	echo "$0 start|stop|restart|status 显示运行时间"
}

if [ "$1" == "" ]; then
	help
elif [ "$1" == "stop" ];then
	stop
elif [ "$1" == "start" ];then
	start
elif [ "$1" == "restart" ];then
	restart
elif [ "$1" == "status" ];then
	status
else
	help
fi